// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../../Commons/GameEvent";
import AudioManager, { AudioEffectType } from "../../Managers/AudioManager";
import SaveManager from "../../Managers/SaveManager";
import UIManager from "../../Managers/UIManager";
import TaskEntity from "./TaskEntity";
import TaskTemplate from "./TaskTemplate";

export enum TaskEvent {
    RECEIVETASKREWARD_EVENT = "receiveTaskReward_event"
}

const { ccclass, property } = cc._decorator;

@ccclass
export default class TaskManager implements IModelManager {

    private static _single: TaskManager = null;
    private taskMap: Map<number, TaskTemplate[]>;
    public static get single(): TaskManager {
        if (TaskManager._single == null) {
            TaskManager._single = new TaskManager();
        }
        return TaskManager._single;
    }


    initConfig(): void {
        this.taskMap = new Map();
        var jsonAss = cc.resources.get("configs/task", cc.JsonAsset);
        var taskArr = [];
        for (const iterator of jsonAss.json) {
            taskArr = this.taskMap.get(iterator.taskType);
            if (taskArr == null) {
                taskArr = [];
                this.taskMap.set(iterator.taskType, taskArr);
            }
            var taskTp = new TaskTemplate();
            taskTp.init(iterator);
            taskArr.push(taskTp);
        }
    }

    getTaskListByType(type: number): TaskTemplate[] {
        var arr = [];
        this.taskMap.forEach((value: TaskTemplate[], key: number, map: Map<number, TaskTemplate[]>) => {
            if (key == type) {
                arr = value;
            }
        }, this);
        return arr;
    }
    isHaveReceive(): boolean {
        for (let i = 0; i < 10; i++) {
            var taskArr = TaskManager.single.getTaskListByType(101 + i);
            var taskEy = TaskManager.single.getTaskEntity(taskArr[0].taskType);
            if (taskEy != null && !taskEy.rewards[taskEy.nestRewardIndex] && taskEy.taskPregress >= taskArr[taskEy.nestRewardIndex].targetNum) {
                return true;
            }
        }

        return false;
    }


    getTaskEntity(tasktype: number): TaskEntity {
        return SaveManager.single.userdata.getTaskEntity(tasktype);
    }

    requestUpdataTaskPregress(tasktype: number, pregress: number) {
        var userdata = SaveManager.single.userdata;
        var taskEy = userdata.getTaskEntity(tasktype);
        if (taskEy == null) {
            taskEy = new TaskEntity();
            taskEy.taskId = tasktype;
            taskEy.taskPregress = 0;
            var arr = this.getTaskListByType(tasktype);
            for (let i = 0; i < arr.length; i++) {
                taskEy.rewards[i] = false;
            }
            userdata.taskList.push(taskEy);
        }
        taskEy.taskPregress += pregress;
        SaveManager.single.saveData();
    }

    requestReceiveTaskReward(tasktype: number, rewardIndex: number): void {
        var userdata = SaveManager.single.userdata;
        var taskEy = userdata.getTaskEntity(tasktype);
        if (taskEy == null) {
            return;
        }
        if (taskEy.rewards[rewardIndex]) {
            UIManager.single.showTips("奖励已领取。");
            return;
        }
        var arr = this.getTaskListByType(tasktype);
        if (taskEy.taskPregress < arr[rewardIndex].targetNum) {
            UIManager.single.showTips("领奖条件未达成。");
            return;
        }


        taskEy.rewards[rewardIndex] = true;

        userdata.diamond += arr[rewardIndex].diamondNum;
        AudioManager.single.playEffect(AudioEffectType.goldDrop);

        UIManager.single.showTips("钻石 +" + arr[rewardIndex].diamondNum + "。");
        SaveManager.single.saveData();

        GameEvent.eventNode.emit(TaskEvent.RECEIVETASKREWARD_EVENT);
        GameEvent.eventNode.emit(GameEvent.CURRENCYUPDATA_EVENT);
    }
}
